package titt.server.func;

import titt.TITT;
import titt.head.request.MethodHead;
import titt.io.IOFactory;
import titt.io.socket.TITTSocket;
import titt.key.TITTKey;
import titt.server.os.ObjectServer;
import titt.type.TypedObject;
import titt.util.io.pipe.PipeListener;

public class ServiceRequestHelper implements TITTFunction {

	@Override
	public String getMethodName() {
		return "requestHelper";
	}

	@Override
	public void handle(TITTKey key, MethodHead head, TITTSocket ts, ObjectServer os) throws Exception {
		TypedObject[] params = head.getParams();

		String methodName = params[1].getObject();
				
		String[] listenerKey = null;
		
		if(params.length > 2)
			listenerKey = params[2].getObject();
				
		PipeListener l = listenerKey == null? null : TITT.createProxy(PipeListener.class, listenerKey);
				
		head = new MethodHead(methodName, head.getInternalParams(), new TypedObject[]{params[0]}, head);
		
		os.getServerInternal().request(key, ts, head, IOFactory.createInternalListener(l));


	}

}
